
package model;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

import controller.ServerController;

public class ServerModel {
    
    private ArrayList<ServerThreadModel> clients;
    
    private int port;
    private ServerSocket serverSocket;
    
    public ServerModel(int port) {
        this.port = port;
        clients = new ArrayList<ServerThreadModel>();
    }
    
    public boolean startConnection() {
        try {
            serverSocket = new ServerSocket(port);
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    
    public void acceptNewClient(ServerController serverController) {
        try {
            ServerThreadModel clientThreadModel = new ServerThreadModel(
                    serverSocket.accept(), serverController);
            clients.add(clientThreadModel);
        } catch (IOException e) {
        }
    }
    
    public void closeConnection() {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
